﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using LRSTaxServices.Biz;
using LRSTaxServices.Common;

public partial class AdminLRS_ManageLRSTransactions : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_LRSTransactions = "__LRSTransactions::LRSTransactions";
    private const string PROP_LAYOUT = "__LRSTransactions::UIL";
    private const string PROP_SELECTED_LRSTransactions_ID = "__LRSTransactions::STransactionID";

    #endregion

    #region Public Properties

    public LRSTransactionsCollection DataSourceLRSTransactions
    {
        get { return (SessionMgr.Session[PROP_DATASRC_LRSTransactions] is LRSTransactionsCollection ? (LRSTransactionsCollection)SessionMgr.Session[PROP_DATASRC_LRSTransactions] : (LRSTransactionsCollection)(SessionMgr.Session[PROP_DATASRC_LRSTransactions] = new LRSTransactionsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_LRSTransactions] = value; }
    }

    public int SelectedTransactionID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_LRSTransactions_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_LRSTransactions_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_LRSTransactions_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceLRSTransactions = LRSTransactions.GetAllTransactions();
            LRSTransactionsList1.DataBind(DataSourceLRSTransactions);
            //divexistname.Visible = false;
        }
    }

    #endregion

    #region Control: LRSTransactionsList

    protected void ucLRSTransactionsList_EditRequested(object sender, Controls_TransactionsList.EditRequestedEventArgs evt)
    {
        SelectedTransactionID = evt.TN.TransactionID;
        LRSTransactionsViewEdit1.DataBind(evt.TN);
        SetLayout(LayoutType.Edit);
        //divexistname.Visible = false;
    }


    protected void ucLRSTransactionsList_AddNewRequested(object sender, EventArgs evt)
    {
        //LRSTransactionsViewEdit1.CancelEdit();
        //LRSTransactions newLRSTransactions = LRSTransactions.CreateNew(DataSourceLRSTransactions);
        //SelectedTransactionID = newLRSTransactions.TransactionID;
        //LRSTransactionsViewEdit1.DataBind(DataSourceLRSTransactions.FindByTransactionID(SelectedTransactionID));
        //SetLayout(LayoutType.Edit);
        ////divexistname.Visible = false;
    }


    protected void ucLRSTransactionsList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceLRSTransactions = LRSTransactions.GetAllTransactions();
        LRSTransactionsList1.DataSource = DataSourceLRSTransactions;
    }

    #endregion

    #region Control: LRSTransactionsViewEdit

    protected void ucLRSTransactionsViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        LRSTransactionsViewEdit1.DataSourceLRSTransactions = DataSourceLRSTransactions.FindByTransactionID(SelectedTransactionID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        LRSTransactionsViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceLRSTransactions.FindByTransactionID(SelectedTransactionID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceLRSTransactions.Remove(DataSourceLRSTransactions.FindByTransactionID(SelectedTransactionID));
                SelectedTransactionID = (DataSourceLRSTransactions[0] != null ? DataSourceLRSTransactions[0].TransactionID : 0);
            }
            catch
            {
            }
        }
        LRSTransactionsList1.DataBind(DataSourceLRSTransactions);
        SetLayout(LayoutType.View);
    }
    //protected void btnsave_Click(object sender, ImageClickEventArgs e)
    //{
    //    if (LRSTransactionsViewEdit1.EndEdit())
    //    {
    //        LRSTransactions obj = DataSourceLRSTransactions.FindByTransactionID(SelectedTransactionID);
    //        LRSTransactions.Update(obj);
    //        //Set the feedback text here.....
    //        DataSourceLRSTransactions = LRSTransactions.GetAllLRSTransactions();
    //        LRSTransactionsList1.DataBind(DataSourceLRSTransactions);
    //        SetLayout(LayoutType.View);
    //        Response.AppendHeader("Refresh", "0");
    //        //divexistname.Visible = false;
    //    }
    //    //else
    //    //{
    //    //    divexistname.Visible = true;
    //    //}
    //}


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlLRSTransactionsList.Visible = false;
                pnlLRSTransactionsViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlLRSTransactionsList.Visible = true;
                pnlLRSTransactionsViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion
}
